package com.iyunsir.user.service.impl;

import com.iyunsir.user.controller.query.UserListQuery;
import com.iyunsir.user.entity.User;
import com.iyunsir.user.mapper.UserMapper;
import com.iyunsir.user.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        return userMapper.getUser(user);
    }

    @Override
    public List<User> queryUsers(UserListQuery query) {
        return userMapper.findAll(query);
    }

    @Override
    public User queryUser(Integer userId) {
        return userMapper.findById(userId);
    }

    @Override
    public Long countUsers(UserListQuery query) {
        return userMapper.countAll(query);
    }

    @Override
    public void saveUser(User user) {
        if (null == user.getId()) {
            // 用户密码加密
            user.encodePassword();
            userMapper.insert(user);
        } else {
            // 不修改密码，密码修改功能应该是单独的
            userMapper.updateById(user);
        }
    }

    @Override
    public void deleteUserByIds(String ids) {
        userMapper.deleteUserByIds(ids);
    }
}
